import { handleActions } from 'redux-actions'

const init = {
  /* New */
  teamMemebers: [],
  teams: [],
  /**/

  teamUser: {},
  team: [],
	teamSubordinates: [],
  teamReportees: [],
  teamAncestorChildren: {},
  singleTeam: {}
}

export default handleActions({
  /** New Team Reducer */
  'fetch team memeber': (state, { payload }) => ({
    ...state,
    teamMemebers: [
      ...state.teamMemebers,
      payload
    ]
  }),
  'fetch teams': (state, { payload }) => ({
    ...state,
    teams: payload
  }),
  /**End Of New Reducer */

	'reset team': (state, { payload }) => {
    return {
      ...state,
      ...init
    };
	},

  'fetch team': (state, { payload }) => ({
    ...state,
    singleTeam: payload
  }),
  'update teams': (state, { payload }) => {
    return {
      ...state,
      team: payload,
      createTeam: false,
      deleteTeam: false
    }
  },

	'update subordinate': (state, { payload }) => ({
		...state,
		teamSubordinates: [ payload ],
		transferTeam: false
	}),

  'fetch subordinate': (state, { payload }) => {
    return {
      ...state,
			teamSubordinates: [
				...state.teamSubordinates,
				payload
			]
    }
  },

  'fetch team data': (state, { payload }) => ({
    ...state,
    teams: payload
  }),

  'clear reportees': (state, { payload }) => ({
    ...state,
    teamReportees: state.teamReportees.filter(obj => obj.leader != payload)
  }),

  'fetch reportees': (state, { payload }) => {
    if (!payload.reportees.length) return state
    return {
      ...state,
      teamReportees: [
        ...state.teamReportees,
        payload
      ]
    }
  },

  'reload reportees': (state, { payload }) => ({
    ...state,
    teamReportees: [ payload ],
    updateReportees: false
  }),

  'update reportees': (state, action) => ({
    ...state,
    updateReportees: true
  }),

  'clear team reportees': (state, action) => ({
    ...state,
    teamReportees: []
  }),

  'fetch team ancestor children': (state, { payload }) => ({
    ...state,
    teamAncestorChildren: payload,
    isFetchingTeamAncestorChildren: Symbol('fetch')
  }),

  'fetch team users': (state, { payload }) => ({
    ...state,
    teamUser: payload,
    isFetchingTeamUsers: Symbol('fetch') 
  }),

}, init)
